﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrainManage.Entities.Trade;
using GrainManage.Server.Factory;
using GrainManage.Data.Util;

namespace GrainManage.Server.Business
{
    class TradeBusiness : BaseBusiness, GrainManage.Interface.ITrade
    {

        //************************************************POST************************************************
        public OutputGetTradeType GetTradeType(InputGetTradeType input)
        {
            var result = new OutputGetTradeType();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.TradeTypes = service.GetTradeType(input);
                if (result.TradeTypes.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetDetailByID GetDetailByID(InputGetDetailByID input)
        {
            var result = new OutputGetDetailByID();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                var detail = service.GetDetail(input.TradeID);
                if (detail == null)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    result.TradeDetail = detail;
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetDetail GetDetail(InputGetDetail input)
        {
            var result = new OutputGetDetail();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.TradeDetail = service.GetDetail(input.TradeFilter, input, result, input.Authentication.UserName);
                if (result.TradeDetail.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetTotal GetTotal(InputGetTotal input)
        {
            var result = new OutputGetTotal();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.TotalDetail = service.GetTotal(input.Filter, input, result, input.Authentication.UserName);
                if (result.TotalDetail.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetTotalByArea GetTotalByArea(InputGetTotalByArea input)
        {
            var result = new OutputGetTotalByArea();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.TotalDetail = service.GetTotalByArea(input.Filter, input, result, input.Authentication.UserName);
                if (result.TotalDetail.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetTotalByContact GetTotalByContactArea(InputGetTotalByContact input)
        {
            var result = new OutputGetTotalByContact();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.TotalDetail = service.GetTotalByContactArea(input.TradeFilter, input, result, input.Authentication.UserName);
                if (result.TotalDetail.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputInsert Insert(InputInsert input)
        {
            var result = new OutputInsert();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.TradeID = service.Insert(input.Trade);
                if (result.TradeID > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.InsertFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputUpdate Update(InputUpdate input)
        {
            var result = new OutputUpdate();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                if (service.Update(input.Trade) > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.UpdateFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputDelete Delete(InputDelete input)
        {
            var result = new OutputDelete();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                if (service.Delete(input.TradeID) > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.DeleteFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchInsert BatchInsert(InputBatchInsert input)
        {
            var result = new OutputBatchInsert();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.TradeIDs = service.Insert(input.Trades);
                if (!result.TradeIDs.Contains(0))
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchInsertFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchUpdate BatchUpdate(InputBatchUpdate input)
        {
            var result = new OutputBatchUpdate();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.InvalidTradeIDs = service.Update(input.Trades);
                if (result.InvalidTradeIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchUpdateFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchDelete BatchDelete(InputBatchDelete input)
        {
            var result = new OutputBatchDelete();
            try
            {
                var service = ObjectFactory.GetTradeService(input);
                result.InvalidTradeIDs = service.Delete(input.TradeIDs);
                if (result.InvalidTradeIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchDeleteFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }
    }
}
